package com.lgqz.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.adapter.DiscoverAdapter;
import com.lgqz.himalayafm.bean.Album;
import com.lgqz.himalayafm.bean.AlbumDetails;
import com.lgqz.himalayafm.http.HttpService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseFragment extends Fragment implements Callback<AlbumDetails> {


    private SimpleDraweeView mUserIcon;
    private TextView mTitle;
    private TextView mAnchor;
    private TextView mPlay;
    private TextView mUpdate;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    public BaseFragment() {
        // Required empty public constructor
    }

    public static BaseFragment newInstance(long albumId) {
        Bundle args = new Bundle();
        args.putLong("albumId", albumId);
        BaseFragment fragment = new BaseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        long mAlbumId = bundle.getLong("albumId");

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        HttpService service = retrofit.create(HttpService.class);
        service.getAlbumProgram(mAlbumId, "android", "null", "20").enqueue(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_base, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar_detail_base);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        final RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.rl_album);
        toolbar.setLogo(R.drawable.ic_web_back);
        mUserIcon = ((SimpleDraweeView) view.findViewById(R.id.sdv_user_icon_header_base));
        mTitle = ((TextView) view.findViewById(R.id.tv_title_header_base));
        mAnchor = ((TextView) view.findViewById(R.id.tv_anchor_header_base));
        mPlay = ((TextView) view.findViewById(R.id.tv_play_header_base));
        mUpdate = ((TextView) view.findViewById(R.id.tv_state_header_base));

        mTabLayout = (TabLayout) view.findViewById(R.id.tl_fragment_base);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_fragment_base);
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mViewPager.getCurrentItem() == 1) {
                    DrawerLayout parent = (DrawerLayout) relativeLayout.getParent().getParent();
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });
    }

    @Override
    public void onResponse(Call<AlbumDetails> call, Response<AlbumDetails> response) {
        if (response.body().getData().getAlbum() != null) {
            Album album = response.body().getData().getAlbum();
            mUserIcon.setImageURI(album.getCoverMiddle());
            mTitle.setText(album.getTitle());
            mAnchor.setText("主播：" + album.getNickname());
            mPlay.setText("播放：" + album.getPlayTimes());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(new Date(album.getLastUptrackAt()));
            mUpdate.setText("状态：" + format + "更新");

            long count = response.body().getData().getTracks().getTotalCount();
            String[] classify = new String[]{"详情", "节目" + "(" + count + ")"};
            List<Fragment> fragmentList = new ArrayList<>();
            fragmentList.add(AlbumDetailsFragment.newInstance(album.getAlbumId()));
            fragmentList.add(AlbumProgramFragment.newInstance(album.getAlbumId()));
            DiscoverAdapter adapter = new DiscoverAdapter(getFragmentManager(), classify, fragmentList);
            mViewPager.setAdapter(adapter);
            mTabLayout.setupWithViewPager(mViewPager);
        }
    }

    @Override
    public void onFailure(Call<AlbumDetails> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
